unit uApontamento;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uPai_new, Menus, DB, StdCtrls, Buttons, ExtCtrls, MMJPanel, DateUtils,
  FMTBcd, Grids, DBGrids, JvExDBGrids, JvDBGrid, SqlExpr, Provider,
  DBClient;

type
  TfApontamento = class(TfPai_new)
    Label2: TLabel;
    Label4: TLabel;
    edProduto: TEdit;
    edOf: TEdit;
    edProdDescri: TEdit;
    dsDetOf: TDataSource;
    cdsDetOf: TClientDataSet;
    cdsDetOfDOF_CODIGO: TFMTBCDField;
    cdsDetOfDOF_CODIOF: TStringField;
    cdsDetOfDOF_QTDPRG: TFMTBCDField;
    cdsDetOfDOF_STATUS: TStringField;
    cdsDetOfPRO_QTDEMB: TFMTBCDField;
    dspDetOf: TDataSetProvider;
    sqlDetOf: TSQLDataSet;
    sqlDetOfDOF_CODIGO: TFMTBCDField;
    sqlDetOfDOF_CODIOF: TStringField;
    sqlDetOfDOF_QTDPRG: TFMTBCDField;
    sqlDetOfDOF_STATUS: TStringField;
    sqlDetOfPRO_QTDEMB: TFMTBCDField;
    gridProd: TJvDBGrid;
    procedure btnGravarClick(Sender: TObject);
    procedure btnProcurarClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure edProdutoExit(Sender: TObject);
    procedure edOfExit(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    Procedure ConsultaBase(sql: string);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fApontamento: TfApontamento;

implementation

uses udm, uApontProd;

{$R *.dfm}

procedure TfApontamento.btnGravarClick(Sender: TObject);
var str, REtiq, rResp: String;
begin
end;

procedure TfApontamento.btnProcurarClick(Sender: TObject);
var strProd: String;
begin
  if ((edProduto.Text <> '') and (edOf.Text <> '')) then
  begin
    MessageDlg('Informe um Produto ou uma O.F.', mtInformation, [mbOK],0);
    exit;
  end;
  // Abre o Grid com as OFs de Producao (OF Desmembrada)
  strProd := 'SELECT DOF_CODIGO, DOF_CODIOF, DOF_QTDPRG, DECODE(DOF_STATUS, 0, ' +
    QuotedStr('AGUARDANDO') + ',1, ' + QuotedStr('LIBERADA') + ',2,' + QuotedStr('ANDAMENTO') +
    ',3, ' + QuotedStr('ENC. MANUAL.') +  ', 4,' + QuotedStr('ENC. SISTEMA') +
    ') AS DOF_STATUS, PRO_QTDEMB FROM ATS_DETOF, ATS_OF, F_PRODS WHERE COF_CODIOF = DOF_CODIOF ';
  strProd := strProd + ' AND PRO_CODPRO = COF_CODPRO ';

  if (edProduto.Text <> '') then
    strProd := strProd + ' AND COF_CODPRO = ' + QuotedStr(edProduto.Text);

  if (edOf.Text <> '') then
    strProd := strProd + ' AND DOF_CODIOF = ' + QuotedStr(edOf.Text);

  strProd := strProd + ' ORDER BY DOF_CODIGO DESC ';

  //if (edPro
  if (cdsDetOf.Active) then
    cdsDetOf.Close;
  cdsDetOf.CommandText := strProd;
  cdsDetOf.Open;

end;

procedure TfApontamento.ConsultaBase(sql: string);
begin
  if (dm.cdsBusca.Active) then
    dm.cdsBusca.Close;
  dm.cdsBusca.CommandText := Sql;
  dm.cdsBusca.Open;
end;

procedure TfApontamento.FormKeyPress(Sender: TObject; var Key: Char);
begin
  //inherited;
 if (key = #13) then
 begin
   btnGravar.Click;
 end;
end;

procedure TfApontamento.edProdutoExit(Sender: TObject);
begin
  if (edProduto.Text <> '') then
  begin
    ConsultaBase('SELECT PRO_DESCRI FROM F_PRODS WHERE PRO_CODPRO = ' +
      QuotedStr(edProduto.Text));
    edProdDescri.Text := dm.cdsBusca.Fields[0].AsString;
    // Busca se Tem O.F.

  end;
end;

procedure TfApontamento.edOfExit(Sender: TObject);
begin
  if (edOf.Text <> '') then
  begin
      ConsultaBase('SELECT COF_CODPRO, PRO_DESCRI ' +
      ' FROM ATS_OF, F_PRODS ' +
      ' WHERE COF_CODPRO = PRO_CODPRO AND COF_CODIOF = ' +
      edOf.Text);
      if (not dm.cdsBusca.IsEmpty) then
      begin
        edProduto.Text    := dm.cdsBusca.Fields[1].AsString;
        edProdDescri.Text := dm.cdsBusca.Fields[2].AsString;
      end;
  end;

end;

procedure TfApontamento.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  if (cdsDetOf.Active) then
  begin
    fApontProd.RefProd.Text   := edProduto.Text;
    fApontProd.edOf.Text      := edOf.Text;
    fApontProd.EdProduto.Text := edProdDescri.Text;
  end;
end;

end.
